<template>
    <div id="publish">
        <el-container>
            <el-aside width="200px">
                <div>
                    <div class="progressWrap">
                        <div id="progressBar"></div>
                    </div>
                </div>
            </el-aside>
            <el-container>
                <el-header>
                    <div class="imgWrap"><img src="../../assets/images/logo.png" width="195" height="48" alt=""></div>
                    <div class="paperTitle">
                        <span style="color: #3c8dbc">【&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;&nbsp;&nbsp;</span>{{paper.title}}
                        <span style="color: #3c8dbc">&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;&nbsp;&nbsp;】</span>
                    </div>
                    <div class="wechat" @click="show=!show">
                        <i class="fa fa-wechat"></i>
                    </div>
                    <transition name="el-zoom-in-center">
                        <div v-show="show" class="code">
                            <qrcode :value="msg" :options="{width:200}"></qrcode>
                        </div>
                    </transition>
                </el-header>
                <el-main>
                    <el-card v-for="(val,index) in paper.content" :key="val.id">
                        <div slot="header">
                            <span style="color: #3c8dbc">[ </span>{{index+1}}
                            <span style="color: #3c8dbc"> ]</span>
                            <span class="outlineName">{{val.outline_name}}</span>
                        </div>
                        <el-radio-group v-model="val.answer">
                            <el-radio :label="val2.id" v-for="val2 in paper.item" :key="val2.id" @change="progress">{{val2.item_name}}</el-radio>
                        </el-radio-group>
                    </el-card>
                    <el-button type="primary" @click='save'>提交问卷</el-button>
                </el-main>
            </el-container>
        </el-container>
    </div>
</template>

<script>
import VueQrcode from '@chenfengyuan/vue-qrcode';
export default {
    components: {
        'qrcode': VueQrcode,
    },

    data() {
        return {
            paper: {},
            id: '',
            num: 0,
            show: false
        }
    },
    created() {
        this.id = this.$route.params.id
        this.getPaper();
    },
    computed: {
        msg() {
            let tmp = {
                url: this.apiUrl + '/papers/' + this.id,
                id: this.id
            }
            return JSON.stringify(tmp)
        },
    },
    methods: {
        progress() {
            this.num = 0;
            for (let i = 0; i < this.paper.content.length; i++) {
                if (this.paper.content[i].answer) {
                    this.num++;
                }
            }
            var bar = document.getElementById('progressBar');
            bar.style.height = (610 * this.num) / this.paper.content.length + 'px';
            console.log(this.paper.content)
        },
        getPaper() {
            this.axios.get('/papers/' + this.id).then((ret) => {
                this.paper = ret.data.data;
            })
        },
        save() {
            // for (let i = 0; i < this.paper.content.length; i++) {
            //     if (!this.paper.content[i].answer) {
            //         this.$message({
            //             type: 'warning',
            //             message: '选项' + (i + 1) + '未作答!'
            //         });
            //         return;
            //     }
            // }
            console.log(this.paper.content)
            this.axios.post('/answers/add', {
                item: JSON.stringify(this.paper.item),
                content: JSON.stringify(this.paper.content),
                paper_id: this.id
            }).then((ret) => {
                this.$message({
                    type: 'success',
                    message: '提交成功!'
                });
            }).catch(() => {
                this.$message.error('请勿重复作答');
            })
        }
    }
}
</script>

<style lang="less" scoped>
.el-header {
    margin-top: 20px;
    position: relative;
    .imgWrap {
        position: absolute;
    }
    .wechat {
        position: absolute;
        right: 25%;
        top: 50%;
        color: rgb(26, 173, 25);
        :hover{
            color: #3c8dbc;
        }
    }
    .paperTitle {
        height: 60px;
        line-height: 60px;
        width: 80%;
        text-align: center;
        font-size: 20px;
    }
    .code {
        z-index: 100;
        position: absolute;
        background: #fff;
        right: 10px;
        top: 0;
        box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);
    }
}

.el-radio+.el-radio {
    margin-left: 70px;
}

.el-card {
    margin-bottom: 10px;
    width: 80%;
}

.outlineName {
    margin-left: 20px;
}

.progressWrap {
    width: 10px;
    height: 610px;
    background-color: #ddd;
    position: fixed;
    #progressBar {
        transition: all 0.5s;
        height: 0;
        width: 10px;
        border-radius: 0 0 5px 5px;
        background-color: #3c8dbc;
        position: absolute;
        left: 0;
        top: 0;
    }
}

.el-button--primary {
    margin-top: 30px;
    width: 80%;
}
</style>
